Note: There are often multiple ways to answer each question.

For the following questions, explain what went wrong and how the program can be fixed.

Load these packages before starting the problems:

library(ggplot2)
library(dplyr)
  1. We want to add x and y together to get the value of 24:
x <- 14
y <- "10"
x + y
## Error in x + y: non-numeric argument to binary operator
  1. We want to add 1+2 and divide it by 3+4:
((1+2)/(3+4)))
## Error: <text>:1:14: unexpected ')'
## 1: ((1+2)/(3+4)))
##                  ^

For the rest of the questions, we will use the mtcars dataset:

data(mtcars)
  1. We want to save the vector of numbers 1, 2, …, L-1 into the variable x, where L is the number of columns in mtcars.
x <- 1:ncol(mtcars)-1
x
##  [1]  0  1  2  3  4  5  6  7  8  9 10
  1. We want to make a scatterplot of mpg vs. wt:
ggplot(data = mtcars) +
    geom_point(y = mpg, x = wt)
## Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomPoint, : object 'wt' not found
  1. We want to make a histogram of mpg:
ggplot(data = mtcars)
    + geom_histogram(aes(x = mpg))

## Error: Cannot use `+.gg()` with a single argument. Did you accidentally put + on a new line?
  1. We want to make a boxplot of mpg for each value of cyl, overlay it with points, and add a title to the plot:
ggplot(data = mtcars, aes(x = cyl, y = mpg)) +
    geom_boxplot() +
    geom_point()
    labs(title = "Plot of mpg vs. cyl")
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?

## $title
## [1] "Plot of mpg vs. cyl"
## 
## attr(,"class")
## [1] "labels"
  1. We want a scatterplot of qsec vs. wt, but we want all the points to be colored blue:
ggplot(data = mtcars, aes(y = qsec, x = wt)) +
    geom_point(aes(col = "blue"))

  1. We want to create a new column which is miles per quart and display the first 3 rows:
mtcars %>% mutate(miles per quart = mpg / 4) %>% head(n = 3)
## Error: <text>:1:25: unexpected symbol
## 1: mtcars %>% mutate(miles per
##                             ^
  1. We want to compute the mean mpg for each value of gear:
mtcars %>% group_by(gear) %>% summarize(mean = mean)
## Error in summarise_impl(.data, dots): Column `mean` is of unsupported type function
  1. We want to compute the maximum hp and disp in the dataset:
mtcars %>% summarize(max = max(hp, disp))
##   max
## 1 472